package android.slc.appbase.ui.vd;

import android.slc.appbase.entity.ErrorMsg;
import android.slc.appbase.utils.AppIntentUtils;
import android.slc.box.core.property.SimpleWrapObjectProperty;
import android.slc.box.core.virtual_android.ActivityResultCallback;
import android.slc.box.core.virtual_android.Bundle;
import android.slc.utils.entity.ValueWrap;
import javafx.beans.property.ReadOnlyObjectProperty;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public abstract class BaseListCommonVmBox<T extends Serializable> extends BaseDataChangeVmBox {
    protected SimpleWrapObjectProperty<List<T>> dataListOf = new SimpleWrapObjectProperty<>(new ArrayList<>());
    protected SimpleWrapObjectProperty<ErrorMsg> errorMsgOf = new SimpleWrapObjectProperty<>();

    public SimpleWrapObjectProperty<Serializable> showItemClickUiOf = new SimpleWrapObjectProperty<>();

    /**
     * TODO暂未用上
     */
    public final ActivityResultCallback<Bundle> itemClickResultLauncherCallback = result -> {
        if (result != null) {
            Serializable serializable = AppIntentUtils.getDataByBundle(result);
            try {
                notifyRefreshByGlobal((T) serializable);
            } catch (ClassCastException e) {
            }
            onActivityResult(result);
        }
    };

    public ReadOnlyObjectProperty<ValueWrap<List<T>>> getDataListObserver() {
        return dataListOf;
    }

    public List<T> getDataList() {
        return dataListOf.get().getData();
    }

    public ReadOnlyObjectProperty<ValueWrap<ErrorMsg>> getErrorMsgObservable() {
        return errorMsgOf;
    }

    public ErrorMsg getErrorMsg() {
        return errorMsgOf.getValuePlus();
    }

    public ReadOnlyObjectProperty<ValueWrap<Serializable>> getShowItemClickUiOf() {
        return showItemClickUiOf;
    }

    /**
     * 刷新
     */
    public abstract void refresh();

    /**
     * 点击item
     *
     * @param index
     * @param data
     */
    public void itemClick(int index, T data) {
        showItemClickUiOf.setValuePlus(data);
    }

    /**
     * 长按item
     *
     * @param index
     * @param data
     */
    public void itemDoubleClick(int index, T data) {

    }


    /**
     * 相应失败
     *
     * @param errorCode
     * @param errorMessage
     */
    protected void onFailed(int errorCode, String errorMessage) {
        errorMsgOf.setValuePlus(new ErrorMsg(errorCode, errorMessage));
    }

}
